//
//  CJBrandJoinSubController.m
//  Boss
//
//  Created by admin on 2018/9/12.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJBrandJoinSubController.h"
#import "CJBOSSRecommendCell.h"
#import "CJBOSSRecommend.h"
#import "CJCommissionController.h"
#import "CJBrandDetailController.h"

@interface CJBrandJoinSubController ()<UITableViewDelegate,UITableViewDataSource,CJBOSSRecommendCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic)NSUInteger page;
@property (nonatomic)BOOL isRefresh;
@end

@implementation CJBrandJoinSubController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setupUI];
    [self loadData];
    
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.isRefresh=NO;
        weakSelf.page++;
        [weakSelf loadData];
    }];
}

-(void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJBOSSRecommendCell" bundle:nil] forCellReuseIdentifier:@"CJBOSSRecommendCell"];
    self.tableView.estimatedRowHeight = 100;
    
}
-(void)loadData
{
    WEAKSELF
    NSString *sortId = [NSString stringWithFormat:@"%ld",self.sortId];
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [WYHttpServer getMerchantBrandWithSortId:sortId WithCurrentPage:page WithPageSize:@"20" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [weakSelf.dataArray addObjectsFromArray:[CJBOSSRecommend getRemmendModelFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJBOSSRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJBOSSRecommendCell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[NSBundle mainBundle]loadNibNamed:@"CJBOSSRecommendCell" owner:nil options:nil].firstObject;
    }
    CJBOSSRecommend *model =self.dataArray[indexPath.row];
    [cell setRecommend:model];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark CJBOSSRecommendCellDelegate
-(void)clickCommissionBtn:(CJBOSSRecommendCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    CJCommissionController *ccc = [[CJCommissionController alloc]init];
    CJBOSSRecommend *model =self.dataArray[indexPath.row];
    ccc.businessId = model.opportunityId;
    [self.navigationController pushViewController:ccc animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJBOSSRecommend *model =self.dataArray[indexPath.row];
    CJBrandDetailController *bdc = [[CJBrandDetailController alloc]init];
    
    bdc.businessID = model.opportunityId;
    
    [self.navigationController pushViewController:bdc animated:YES];
    
//    CJCommissionController *ccc = [[CJCommissionController alloc]init];
//
//    ccc.businessId = model.opportunityId;
//    [self.navigationController pushViewController:ccc animated:YES];
}



- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, self.view.width, kScreenHeight-self.navHeight-44);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
